import requests
import excel_info_sql
from openpyxl import Workbook
from openpyxl.compat import range
list1 = excel_info_sql.getConditions()
import json
list2 = []
list4 = []
list3 = excel_info_sql.getConditions()

# Platform = "西瓜阅读"
def xigua_time():
    r = requests.session()
    url = "https://www.koread.cn/admin/index/login?url=%2Fadmin%2Findex%2Flogout"
    if "雨季" in  list3[0]["Platform"]:
        username = ""
    else:
        username = ""
    date ={
            "username" : username,
            "password" : ""
    }
    rr = r.post(url = url,data = date)
    for i in range(0,40):
        header={
            "Accept": "application/json, text/javascript, */*; q=0.01",
            "Accept-Encoding": "gzip, deflate, br",
            "Accept-Language": "zh-CN,zh;q=0.9",
            "Connection": "keep-alive",
            "Content-Type": "application/json",
            "Host": "www.koread.cn",
            "Referer": "https://www.koread.cn/admin/referral/referral?addtabs=1",
            "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.84 Safari/537.36",
            "X-Requested-With": "XMLHttpRequest"
        }

        aa = r.get(url="https://www.koread.cn/admin/referral/referral/index?offset={0}&limit=10".format(i*10),headers = header)
        html = json.loads(aa.text)

        bb =html["rows"]
        for i in range(0,len(bb)):
            Novel_name = html["rows"][i]["book"]["name"]
            Public_platform = html["rows"][i]["name"]
            Number_clicks = html["rows"][i]["uv"]
            Recharge_amount = html["rows"][i]["money"]
            number = html["rows"][i]["follow"]
            if str(list3[0]["date_time"]) in Public_platform:
                list4 = [Public_platform, Novel_name, Number_clicks, Recharge_amount, number]
                list2.append(list4)
    return list2

def test1():
    list1 = xigua_time()
    Platform = list3[0]["Platform"]
    Public_platform_list = []
    Novel_name_list = []
    Number_clicks_list = []
    Recharge_amount_list = []
    number_list = []
    wb = Workbook()
    dest_filename = '{0}{1}.xlsx'.format(str(list3[0]["date_time"]), Platform)
    ws1 = wb.active
    ws1.title = "names"
    ws1.column_dimensions['A'].width = 20.0
    ws1.column_dimensions['B'].width = 16.0
    ws1.column_dimensions['C'].width = 12.0
    ws1.column_dimensions['D'].width = 12.0
    ws1.column_dimensions['E'].width = 12.0
    for i in range(len(list1)):
        Public_platform = list1[i][0]
        Novel_name = list1[i][1]
        Number_clicks = list1[i][2]
        Recharge_amount = list1[i][3]
        number = list1[i][4]
        Public_platform_list.append(Public_platform)
        Novel_name_list.append(Novel_name)
        Number_clicks_list.append(Number_clicks)
        Recharge_amount_list.append(Recharge_amount)
        number_list.append(number)

    for row in range(2, len(Public_platform_list) + 2):
        ws1['A1'] = u"推广名称"
        ws1['B1'] = u"小说名"
        ws1['C1'] = u"阅读人数"
        ws1['D1'] = u"充值"
        ws1['E1'] = u"关注人数"

        ws1['A{0}'.format(row)] = Public_platform_list[row - 2]
        ws1['B{0}'.format(row)] = Novel_name_list[row - 2]
        ws1['C{0}'.format(row)] = Number_clicks_list[row - 2]
        ws1['D{0}'.format(row)] = float(Recharge_amount_list[row - 2])
        ws1['E{0}'.format(row)] = number_list[row - 2]
    wb.save(filename=dest_filename)

if __name__ == '__main__':
    test1()


            # if str(list1[0]["date_time"]) in Public_platform:
        #     a = biz_mysql.xigua_select_novel_mysql(Novel_name=Novel_name, Public_platform=Public_platform)
        #     if a == 1:
        #         biz_mysql.xigua_Update_novel_mysql(Public_platform, Novel_name, Number_clicks, Recharge_amount, number)
        #         print("西瓜阅读 更新数据")
        #     else:
        #         biz_mysql.xigua_insert_novel_mysql(Public_platform, Novel_name, Number_clicks, Recharge_amount, number)
        #         print("西瓜阅读 插入数据")
        # else:
        #     pass